	// pages/trade/trade.js
var app = getApp()

const util = require('../../utils/util.js')

Page({

  /**				
   * 页面的初始数据
   */
	data: {
		navbar: ['我的发布', '我的申请', '历史交易'],
		urls: [
			'/pages/dispatchDetail/dispatchDetail', 
			'/pages/applyDetail/applyDetail', 
			'/pages/hisTradeInfo/hisTradeInfo'],
		currentTab: 0,

		trades:[]
	},

	onShow: function (options) {
		// util.getTrades(app.globalData.user.userId, this)		
		util.getTrades(app.globalData.userId, this)	
	},

	/**
	 * 顶部标签的切换
	 */
	navbarTap: function (e) {
		this.setData({
			currentTab: e.currentTarget.dataset.idx
		})
	},

	/**
	 * 点击卡片之后的反应
	 */
	tradeTab: function (e){
		var index = e.currentTarget.dataset.idx
		app.globalData.trade = this.data.trades[this.data.currentTab][index]
		wx.navigateTo({
			url: this.data.urls[this.data.currentTab]
		})
	},

	/**
	 * 跳转到申请列表
	 */
	applyList: function(e) {
		var index = e.currentTarget.dataset.idx
		app.globalData.trade = this.data.trades[this.data.currentTab][index]
		wx.navigateTo({
			url: '../applyList/applyList',
		})
	},

	/**
 * 单方完成
 */
	finishSingle: function (e) {
		var index = e.currentTarget.dataset.idx
		var trade = this.data.trades[this.data.currentTab][index]
		app.globalData.trade = this.data.trades[this.data.currentTab][index]
		util.changeIdent(trade.indent.indentId, '完成订单', function () {
			wx.redirectTo({
				url: '/pages/addComment/addComment'
			})
		})
	},

	/**
	 * 完成交易
	 */
	finishDouble: function (e) {
		var index = e.currentTarget.dataset.idx
		var trade = this.data.trades[this.data.currentTab][index]
		app.globalData.trade = this.data.trades[this.data.currentTab][index]
		util.changeIdent(trade.indent.indentId, '完成订单', function () {
			wx.redirectTo({
				url: '/pages/addComment/addComment'
			})
		})
	},

	/**
	 * 取消申请
	 */
	cancleApply: function (e) {
		var index = e.currentTarget.dataset.idx
		var trade = this.data.trades[this.data.currentTab][index]
    console.log(trade)
		util.deleteIndent(trade.indent.indentId, function(self){
			// util.getTrades(app.globalData.user.userId, this)		
			util.getTrades(app.globalData.userId, self)	
		}, this)
		
	},

	/**
	 * 删除记录
	 */
	deleteRecord: function (e) {
		var index = e.currentTarget.dataset.idx
		var trade = this.data.trades[this.data.currentTab][index]
		util.deleteIndent(trade.indent.identId, function (self) {
			// util.getTrades(app.globalData.user.userId, this)		
			util.getTrades(app.globalData.userId, self)	
		}, this)
	}
})